<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择状态" clearable size="small">
          <el-option
            v-for="dict in stateOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="联网状态" prop="netState">
        <el-select v-model="queryParams.netState" placeholder="请选择联网状态" clearable size="small">
          <el-option
            v-for="dict in netStateOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['instrument:instrument:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['instrument:instrument:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['instrument:instrument:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['instrument:instrument:export']"
        >导出
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-finished"
          size="mini"
          :disabled="multiple"
          @click="handleCalibration"
          v-hasPermi="['instrument:instrument:calibration']"
        >批量定标
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-refresh"
          size="mini"
          :disabled="multiple"
          @click="handleVersionUpdate"
          v-hasPermi="['instrument:instrument:versionUpdate']"
        >批量版本更新
        </el-button>
      </el-col>
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="warning"-->
      <!--          icon="el-icon-download"-->
      <!--          size="mini"-->
      <!--          @click="handlePubClick"-->
      <!--          v-hasPermi="['instrument:project:export']"-->
      <!--        >测试发布主题</el-button>-->
      <!--      </el-col>-->
    </el-row>

    <el-table v-loading="loading" :data="instrumentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <!--      <el-table-column label="自增ID" align="center" prop="id"/>-->
      <el-table-column label="设备编号" align="center" prop="name"/>
      <el-table-column label="机型" align="center" prop="model" :formatter="modelFormat"/>
      <el-table-column label="状态" align="center" prop="state" :formatter="stateFormat"/>
      <el-table-column label="联网状态" align="center" prop="netState" :formatter="netStateFormat"/>
      <el-table-column label="操作" width="350" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['instrument:instrument:edit']"
          >修改
          </el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-finished"
            @click="handleCalibration(scope.row)"
            v-hasPermi="['instrument:instrument:calibration']"
          >定标
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-refresh"
            @click="handleVersionUpdate(scope.row)"
            v-hasPermi="['instrument:instrument:versionUpdate']"
          >版本更新
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-switch-button"
            @click="handlePowerOff(scope.row)"
            v-hasPermi="['instrument:instrument:powerOff']"
          >关机
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['instrument:instrument:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改仪器信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="设备编号" prop="name">
          <el-input v-model="form.name" placeholder="请输入设备编号"/>
        </el-form-item>
        <el-form-item label="机型">
          <el-select v-model="form.model" placeholder="请选择机型">
            <el-option
              v-for="dict in machineTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="软件版本">
          <el-select v-model="form.ver" placeholder="请选择软件版本">
            <el-option
              v-for="dict in verOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="版本日期" prop="verDate">
          <el-date-picker clearable size="small" style="width: 200px"
                          v-model="form.verDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择版本日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.state">
            <el-radio
              v-for="dict in stateOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{dict.dictLabel}}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="联网状态">
          <el-radio-group v-model="form.netState">
            <el-radio
              v-for="dict in netStateOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{dict.dictLabel}}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="上线时间" prop="onlineTime">
          <el-date-picker clearable size="small" style="width: 200px"
                          v-model="form.onlineTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择上线时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    addInstrument,
    calibrationInstrument,
    delInstrument,
    exportInstrument,
    getInstrument,
    listInstrument,
    powerOffInstrument,
    pubTest,
    updateInstrument,
    versionUpdateInstrument
  } from '@/api/instrument/instrument'

  export default {
    name: 'Instrument',
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 仪器信息表格数据
        instrumentList: [],
        // 弹出层标题
        title: '',
        // 是否显示弹出层
        open: false,
        // 型号字典
        machineTypeOptions: [],
        // 软件版本字典
        verOptions: [],
        // 状态字典
        stateOptions: [],
        // 联网状态字典
        netStateOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          state: undefined,
          netState: undefined
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          name: [
            { required: true, message: '设备编号不能为空', trigger: 'blur' }
          ],
          model: [
            { required: true, message: '机型不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    created() {
      this.getList()
      this.getDicts('machine_type').then(response => {
        this.machineTypeOptions = response.data
      })
      this.getDicts('instrument_version').then(response => {
        this.verOptions = response.data
      })
      this.getDicts('instrument_state').then(response => {
        this.stateOptions = response.data
      })
      this.getDicts('instrument_net_state').then(response => {
        this.netStateOptions = response.data
      })
    },
    methods: {
      /** 查询仪器信息列表 */
      getList() {
        this.loading = true
        listInstrument(this.queryParams).then(response => {
          this.instrumentList = response.rows
          this.total = response.total
          this.loading = false
        })
      },
      // 型号字典翻译
      modelFormat(row, column) {
        return this.selectDictLabel(this.machineTypeOptions, row.model)
      },
      // 软件版本字典翻译
      verFormat(row, column) {
        return this.selectDictLabel(this.verOptions, row.ver)
      },
      // 状态字典翻译
      stateFormat(row, column) {
        return this.selectDictLabel(this.stateOptions, row.state)
      },
      // 联网状态字典翻译
      netStateFormat(row, column) {
        return this.selectDictLabel(this.netStateOptions, row.netState)
      },
      // 取消按钮
      cancel() {
        this.open = false
        this.reset()
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          name: undefined,
          model: undefined,
          ver: undefined,
          verDate: undefined,
          registerTime: undefined,
          state: '0',
          netState: '0',
          remark: undefined,
          onlineTime: undefined
        }
        this.resetForm('form')
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1
        this.getList()
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm('queryForm')
        this.handleQuery()
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length != 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset()
        this.open = true
        this.title = '添加仪器信息'
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset()
        const id = row.id || this.ids
        getInstrument(id).then(response => {
          this.form = response.data
          this.open = true
          this.title = '修改仪器信息'
        })
      },
      /** 提交按钮 */
      submitForm: function() {
        this.$refs['form'].validate(valid => {
          if (valid) {
            if (this.form.id != undefined) {
              updateInstrument(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess('修改成功')
                  this.open = false
                  this.getList()
                } else {
                  this.msgError(response.msg)
                }
              })
            } else {
              addInstrument(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess('新增成功')
                  this.open = false
                  this.getList()
                } else {
                  this.msgError(response.msg)
                }
              })
            }
          }
        })
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids
        this.$confirm('是否确认删除仪器信息编号为"' + ids + '"的数据项?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return delInstrument(ids)
        }).then(() => {
          this.getList()
          this.msgSuccess('删除成功')
        }).catch(function() {
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams
        this.$confirm('是否确认导出所有仪器信息数据项?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return exportInstrument(queryParams)
        }).then(response => {
          this.download(response.msg)
        }).catch(function() {
        })
      },
      /**定标操作 */
      handleCalibration(row) {
        const ids = row.id || this.ids
        this.$confirm('是否确认下发定标数据更新指令?', '信息', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return calibrationInstrument(ids)
        }).then(() => {
          this.msgSuccess('定标成功')
        }).catch(function() {
        })
      },
      /**软件版本更新操作*/
      handleVersionUpdate(row) {
        const ids = row.id || this.ids
        this.$confirm('是否确认下发软件更新指令?', '信息', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return versionUpdateInstrument(ids)
        }).then(() => {
          this.msgSuccess('更新成功')
        }).catch(function() {
        })
      },
      /**仪器关机操作*/
      handlePowerOff(row) {
        const ids = row.id || this.ids
        let state, netState
        let $that = this
        this.$confirm('是否确认下发仪器关机指令?', '信息', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          $that.stateOptions.filter(item => {
            if (item.dictLabel === '关机') {
              state = item.dictValue
            }
          })
          $that.netStateOptions.filter(item => {
            if (item.dictLabel === '离线') {
              netState = item.dictValue
            }
          })
          const query = {
            state: state,
            netState: netState
          }
          return powerOffInstrument(ids, query)
        }).then(() => {
          this.getList()
          this.msgSuccess('关机成功')
        }).catch(function() {
        })
      },
      handlePubClick() {
        pubTest('test').then(res => {
          console.log('res:', res)
        })
      }
    }
  }
</script>
